package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyComment;
import cn.wolfcode.trip.base.query.DailyCommentQueryObject;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyCommentService;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dailies")
public class DailyController {
    @Autowired
    private IDailyCommentService dailyCommentService;

    @Autowired
    private IDailyService dailyService;

    //查询所有的日报---前台日报列表
    //如果不需要分页请设置pageSize=0
    @GetMapping
    public PageInfo listDailies(DailyQueryObject qo){
        qo.setOrderBy("d.num desc");
        PageInfo pageInfo = dailyService.query(qo);
        return pageInfo;
    }

    @GetMapping("/{dailyId}/comments")
    public PageInfo getDailyComment(DailyCommentQueryObject qo){
        qo.setOrderBy("dc.createTime desc");
        PageInfo pageInfo = dailyCommentService.query(qo);
        return pageInfo;
    }

    @PostMapping
    public JSONResult save(DailyComment comment){
        JSONResult result=new JSONResult();
        dailyCommentService.saveOrUpdate(comment);
        return result;
    }

    @GetMapping("{id}")
    public Daily queryDailyById(@PathVariable Long id){
       Daily daily= dailyService.queryDailyById(id);
       return daily;
    }

    @PostMapping("browse/{id}")
    public JSONResult updateNum(@PathVariable Long id){
        dailyService.updateNum(id);
        return new JSONResult();
    }

}
